package cn.fanxin.utils;

import cn.fanxin.tools.MyException;

import java.sql.*;
import java.util.Properties;

/**
 * @Author: china-fanxin
 * @Website: https://fan-xin.cn
 * @Date: 2022/4/8 21:01
 * @Description: MySQL的JDBC工具类
 */
public class MyJdbcUtils {

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 根据配置文件获取Analytic DB的连接对象
     * @Date: 2022/11/23 11:11
     * @Param: [configProp]
     * @return: java.sql.Connection
     */
    public static Connection getADBConnection(Properties configProp){
        Connection conn = null;
        //获取Analytic DB连接配置信息
        String url = configProp.getProperty("adbUrl");
        String user = configProp.getProperty("selectAdbUser");
        String password = configProp.getProperty("selectAdbPassword");
        String driver = configProp.getProperty("adbDriver");
        try {
            Class.forName(driver).newInstance();
            Properties connectionProps = new Properties();
            connectionProps.put("user", user);
            connectionProps.put("password", password);
            conn = DriverManager.getConnection(url,connectionProps);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 释放连接资源
     * @Date: 2022/4/7 11:01
     * @Param: [rs, stmt, conn]
     * @return: void
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 释放连接资源
     * @Date: 2022/4/7 11:01
     * @Param: [rs, stmt, conn]
     * @return: void
     */
    public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 释放连接资源
     * @Date: 2022/4/7 16:29
     * @Param: [rs, stmt, conn]
     * @return: void
     */
    public static void close(ResultSet rs, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Author: china-fanxin
     * @Website: https://fan-xin.cn
     * @Description: 释放连接资源
     * @Date: 2022/4/7 16:29
     * @Param: [rs, stmt, conn]
     * @return: void
     */
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
